//
//  B_SearchListVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/11.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_SearchListVC.h"
#import "MainSelectTypeViewController.h"
#import "B_SearchTopicVC.h"
#import "B_SearchActivityVC.h"
#import "B_SearchUserVC.h"

#import "B_SearchTopicModel.h"
#import "B_SearchUserModel.h"

@interface B_SearchListVC ()<UISearchBarDelegate>
@property (nonatomic, strong) UIView * backView;
@property (nonatomic, strong) UISearchBar * searchBar;
@property (nonatomic, strong) MainSelectTypeViewController * mainPage;

@property (nonatomic, strong) B_SearchTopicVC * topic;
@property (nonatomic, strong) B_SearchActivityVC * activity;
@property (nonatomic, strong) B_SearchUserVC * user;

@end

@implementation B_SearchListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSearchNavigation];
    self.view.backgroundColor = [UIColor color_main_background_color];
    self.mainPage.view.hidden = NO;
}

/**
 设置搜索导航栏
 */
- (void) setSearchNavigation {
    WeakSelf(weSelf, self);
    [self setNavigationBarBackgroundWithImage:[UIImage getImageWithColor:[UIColor color_content_background_color] alp:1]];
    [self setRightTextBarButtonItemWithFrame:CGRectZero title:@"取消" titleColor:[UIColor color_text_light_gray] backImage:@"" selectBackImage:@"" action:^(GUIButton *button) {
        [weSelf.searchBar endEditing:YES];
        [weSelf dismissViewControllerAnimated:YES completion:nil];
    }];
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 80, 32)];
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 80, 32)];
    UIImage * searchBarBg = [[UIImage alloc] GetImageWithColor:[UIColor color_main_background_color] alp:1 andHeight:32];
    
    //设置背景图片
    [_searchBar setBackgroundImage:searchBarBg];
    _searchBar.layer.masksToBounds = YES;
    _searchBar.layer.cornerRadius = 2;
    //设置文本框背景
    [_searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
    self.navigationItem.titleView = _backView;
    [_backView addSubview:_searchBar];
    /// 修改字体颜色
    UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
    searchField.textColor =  [UIColor color_text_light_gray];
    searchField.font = xgSystemFont(15);
    [searchField setValue: xgTextGlodColor forKeyPath:@"_placeholderLabel.textColor"];
    searchField.placeholder = @"搜你想搜的";
    /// 修改放大镜
    UIImage *image = [UIImage imageNamed:@"B_topic_home_search"];
    UIImageView *iconView = [[UIImageView alloc] initWithImage:image];
    iconView.frame = CGRectMake(0, 0, image.size.width + 7, image.size.height);
    iconView.contentMode = UIViewContentModeLeft;
    iconView.layer.masksToBounds = YES;
    searchField.leftView = iconView;
    /// 修改光标颜色
    _searchBar.tintColor = [UIColor color_text_light_gray];;
    _searchBar.delegate = self;
    
    [_searchBar becomeFirstResponder];
}

/**
 TODO:搜索按钮的点击事件
 
 @param searchBar 搜索searchBar
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    if (searchBar.text.length == 0 || ISNULL(searchBar.text)) {
        [UIView xg_toast:@"请输入搜索内容!"];
        return ;
    }
    /// 收回键盘,在这里执行搜索
    [searchBar resignFirstResponder];
    [self searchMessage];
}

- (MainSelectTypeViewController *)mainPage {
    if (!_mainPage) {
//        WeakSelf(weSelf, self);
        _mainPage = [[MainSelectTypeViewController alloc] initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, SCREEN_HEIGHT_X - 64 - 5) titleArray:nil];
        [self.view addSubview:self.mainPage.view];
        [self addChildViewController:_mainPage];
        _mainPage.titleArray = @[@"话题", @"活动", @"用户"];
        B_SearchTopicVC * topic = [[B_SearchTopicVC alloc] init];
        B_SearchActivityVC * activity = [[B_SearchActivityVC alloc] init];
        B_SearchUserVC * user = [[B_SearchUserVC alloc] init];
        
        _mainPage.viewControllers = @[topic, activity, user];
        _topic = topic;
        _activity = activity;
        _user = user;
        _mainPage.block = ^(NSInteger index) {
            
        };
    }
    return _mainPage;
}


/**
 TODO:搜索方法
 */
- (void) searchMessage {
    /// 搜索话题
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/search/topicSearch?str=%@", self.searchBar.text] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            [weSelf.topic.dataArray removeAllObjects];
            for (NSDictionary * dictionary in data[@"values"]) {
                B_SearchTopicModel * model = [B_SearchTopicModel mj_objectWithKeyValues:dictionary];
                [weSelf.topic.dataArray addObject:model];
            }
            [weSelf.topic.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
    
    /// TODO:搜索用户
    
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/search/userSearch?user=%@", self.searchBar.text] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            [weSelf.topic.dataArray removeAllObjects];
            for (NSDictionary * dictionary in data[@"values"]) {
                B_SearchUserModel * model = [B_SearchUserModel mj_objectWithKeyValues:dictionary];
                [weSelf.user.dataArray addObject:model];
            }
            [weSelf.user.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
    
    /// TODO:搜索活动
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/search/activitySearch?activityStr=%@", self.searchBar.text] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            [weSelf.activity.dataArray removeAllObjects];
            for (NSDictionary * dictionary in data[@"values"]) {
                B_SearchTopicModel * model = [B_SearchTopicModel mj_objectWithKeyValues:dictionary];
                [weSelf.activity.dataArray addObject:model];
            }
            [weSelf.activity.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
